package com.example.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.pojo.TCustomer;
import com.example.mapper.TCustomerMapper;
import com.example.service.ITCustomerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.util.AssertUtil;
import com.example.util.PhoneUtil;
import com.example.vo.ResultVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jayway.jsonpath.internal.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.jayway.jsonpath.internal.Utils;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wsl
 * @since 2022-04-24
 */
@Service
public class TCustomerServiceImpl extends ServiceImpl<TCustomerMapper, TCustomer> implements ITCustomerService {
    @Autowired
    private TCustomerMapper tCustomerMapper;

    /**
     * 查询所有
     */
    @Override
    public List<TCustomer> selectAll(){
        QueryWrapper<TCustomer> wrapper = new QueryWrapper<TCustomer>().orderByDesc("id");
        return tCustomerMapper.selectList(wrapper);
    }

    /**
     * 客户信息添加
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public ResultVo addCustomer(TCustomer tCustomer){
        //1、参数校验
        checkCustomerParams(tCustomer.getName(),tCustomer.getPhone(),tCustomer.getFr());
        //设置默认值
        //State流失状态         (0=未流失，1=已流失)
        tCustomer.setState(0);
        //isValid是否有效      (0=无效，1=有效)
        tCustomer.setIsValid(1);
        //createDate创建时间    默认为当前系统时间
        tCustomer.setCreateDate(new Date());
        //updateDate更新时间    默认为当前系统时间
        tCustomer.setUpdateDate(new Date());
        //客户编号
        String khno = "KH" +System.currentTimeMillis();
        tCustomer.setKhno(khno);
        //3、执行添加操作，判断受影响的行数
        int i = baseMapper.insert(tCustomer);
        return i > 0 ? ResultVo.ok("添加成功") : ResultVo.error("添加失败");
    }
    //校验参数
    private void checkCustomerParams(String name, String phone, String fr) {
        //Name客户名名称         非空
        AssertUtil.isTrue(Utils.isEmpty(name),"客户名不能为空！！");
        //Fr法人             非空
        AssertUtil.isTrue(Utils.isEmpty(fr),"法人不能为空！！");
        //Phone联系号码          非空
        AssertUtil.isTrue(Utils.isEmpty(phone),"联系电话不能为空！！");
        //Phone联系号码             非空，手机号码格式正确
        AssertUtil.isTrue(!PhoneUtil.isMobile(phone),"手机号码格式不正确！！");

    }

    /**
     * 客户信息修改
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public ResultVo updateCustomer(TCustomer tCustomer){
        //1、参数校验
        checkCustomerParams(tCustomer.getName(),tCustomer.getPhone(),tCustomer.getFr());
        //设置默认值
        tCustomer.setUpdateDate(new Date());
        //3、执行修改操作，判断受影响的行数
        int i = tCustomerMapper.updateById(tCustomer);
        return i != 1 ? ResultVo.error("修改失败") : ResultVo.ok("修改成功");
    }


    /**
     * 无条件分页
     * @param pageNo  当前页码
     * @param pageSize 每页条数
     */
    @Override
    public PageInfo<TCustomer> electByPager(Integer pageNo, Integer pageSize, String name, String khno, String level, String area, String xyd, String fr, Integer state, Integer isValid){
        //1、启动分页
        PageHelper.startPage(pageNo,pageSize);

        QueryWrapper<TCustomer> wrapper = new QueryWrapper<TCustomer>().orderByDesc("id");
        wrapper.like(!Utils.isEmpty(name),"name",name)
                .like(!Utils.isEmpty(khno),"khno",khno)
                .like(!Utils.isEmpty(level),"level",level)
                .like(!Utils.isEmpty(area),"area",area)
                .like(!Utils.isEmpty(xyd),"xyd",xyd)
                .like(!Utils.isEmpty(fr),"fr",fr)
                .eq(state!=null,"state",state)
                .eq(isValid!=null,"is_valid",isValid);
        //2、紧接着查询操作
        List<TCustomer> list = baseMapper.selectList(wrapper);
        //3、组装
        return new PageInfo<>(list);
    }

}
